package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/response"
)

const dir = "./static/"

type UploadController struct {
	db *gorm.DB
}

// Upload 图片上传（单个）
func (receiver UploadController) Upload(ctx *gin.Context) {
	file, _ := ctx.FormFile("file")
	//截取文件格式(例如213.1.jpg -> 获取为.jpg)
	//todo 这里我不想改文件名了
	//suffixIndex := strings.LastIndex(file.Filename, ".")
	//suffixName := file.Filename[suffixIndex: len(file.Filename)]

	//首先判断文件夹是否存在

	//前面是文件，后面是文件命名，默认会保存到整个项目目录下方(我这放到了static文件夹下面)
	err := ctx.SaveUploadedFile(file, dir+file.Filename)
	if err != nil {
		response.Failure(ctx, nil, err.Error())
	}
}

// MultiUpload 图片上传（多个，最多五个）
func (receiver UploadController) MultiUpload(ctx *gin.Context) {

}

func NewUploadController() UploadController {
	db := common.GetDB()
	return UploadController{db: db}
}
